package com.dqhl.community.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.dqhl.community.R;
import com.dqhl.community.adapter.CommunityNoticeAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.CommunityNotice;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LIENLIN on 2016/8/30.
 * 社区公告
 */
public class CommunityNoticeActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ImageView mIvback;
    private TextView mTvTitle;
    private ListView mLvNotice;
    private TextView tv_no_info;
    private List<CommunityNotice> communityNoticeList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_community_notice);

        initView();

        getData();
    }

    private void getData() {
        showCustomLoadBar("加载中……");
        RequestParams params = new RequestParams(Config.Api.noticeList);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getInfo(result, "data");
                if (errCode == 0) {
                    mLvNotice.setVisibility(View.VISIBLE);
                    tv_no_info.setVisibility(View.GONE);
                    communityNoticeList = new ArrayList<CommunityNotice>(
                            JSONArray.parseArray(data, CommunityNotice.class)
                    );
                    if (communityNoticeList.size() <= 0) {
                        mLvNotice.setVisibility(View.GONE);
                        tv_no_info.setVisibility(View.VISIBLE);
                        tv_no_info.setText(R.string.error_no_info);
                    } else {

                        Dlog.e("communityNoticeList:" + communityNoticeList.toString());
                        CommunityNoticeAdapter communityNoticeAdapter = new CommunityNoticeAdapter(
                                CommunityNoticeActivity.this, communityNoticeList
                        );
                        mLvNotice.setAdapter(communityNoticeAdapter);
                        mLvNotice.setOnItemClickListener(noticeItemClickListener);
                    }
                } else {
                    mLvNotice.setVisibility(View.GONE);
                    tv_no_info.setVisibility(View.VISIBLE);
                    tv_no_info.setText(R.string.error_server_connection);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Dlog.e(ex.toString());
                Dlog.e("网络连接错误");
                mLvNotice.setVisibility(View.GONE);
                tv_no_info.setVisibility(View.VISIBLE);
                tv_no_info.setText(R.string.error_net_connection);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    AdapterView.OnItemClickListener noticeItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Dlog.e("Clicked:" + i);
            String noticeId = communityNoticeList.get(i).getId();
            Bundle bundle = new Bundle();
            bundle.putString("notice_id", noticeId);
            overlay(CommunityNoticeDetailActivity.class, bundle);
        }
    };

    private void initView() {
        mIvback = (ImageView) findViewById(R.id.iv_top_back);
        mTvTitle = (TextView) findViewById(R.id.tv_top_title);
        mLvNotice = (ListView) findViewById(R.id.lv_notice);
        tv_no_info = (TextView) findViewById(R.id.tv_no_info);

        mIvback.setVisibility(View.VISIBLE);
        mTvTitle.setText(R.string.tv_notice_top_title);

        mIvback.setOnClickListener(this);
        mLvNotice.setOnItemClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //TODO 跳转到公告详情页
        overlay(CommunityNoticeDetailActivity.class);
    }
}
